package com.lucene.controller;

import com.alibaba.fastjson.JSONObject;
import com.lucene.service.LuceneService;
import com.lucene.service.ProjectService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * Created by zhangzhen on 22/09/2017.
 */

@RestController
@RequestMapping(value = "/lucene")
public class LuceneController {
    private static Logger logger = Logger.getLogger(LuceneController.class);

    @Autowired
    private ProjectService projectService;

    @Autowired
    private LuceneService luceneService;

    @RequestMapping(value = "/clear")
    public String clearIndices(){
        logger.info("clear all indices.");
        JSONObject object = new JSONObject();
        try {
            int rs = luceneService.deleteAllIndex();
            if (rs == 0){
                object.put("code",0);
                object.put("msg","remove all indices success.");
            }else {
                object.put("code",1);
                object.put("msg","remove all indices fail.");
            }
        }catch (IOException e){
            object.put("code",2);
            object.put("msg","exception.");
        }
        return object.toJSONString();
    }

    @RequestMapping(value = "/init")
    public String initIndices(){
        logger.info("init indices.");
        JSONObject object = new JSONObject();
        try {
            int rs = luceneService.initDBItemIndex();
            if (rs == 0){
                object.put("code",0);
                object.put("msg","init indices success.");
            }else {
                object.put("code",1);
                object.put("msg","init indices fail.");
            }
        }catch (IOException e){
            e.printStackTrace();
            object.put("code",2);
            object.put("msg","exception.");
        }
        return object.toJSONString();
    }
}
